home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / c / ExtrasLib.lha / ExtrasLib / Source / MAMem.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  5.3 KB  |  207 lines

  1. #include <proto/exec.h>
  2. #include <clib/extras_protos.h>
  3.  
  4. /****** extras.lib/MultiAllocMemA ******************************************
  5. *
  6. *   NAME
  7. *       MultiAllocMemA -- Multiple AllocMem.
  8. *       MultiAllocMem -- varargs stub.
  9. *
  10. *   SYNOPSIS
  11. *       succes = MultiAllocMemA(Flags, MemTagList)
  12. *
  13. *       BOOL MultiAllocMem(ULONG, struct MemTag *);
  14. *
  15. *       succes = MultiAllocMemA(Flags, MemTag)
  16. *
  17. *       BOOL MultiAllocMemA(Flags, ULONG, ...);
  18. *
  19. *   FUNCTION
  20. *       Attempt to allocate one or more memory chunks
  21. *       using AllocMem.
  22. *
  23. *   INPUTS
  24. *       Flags - MA_FAILSIZE0: fail all allocations if any
  25. *               have a size of 0.  if your application will be
  26. *               allocating memory of dynamic sizes, and if
  27. *               you want allocations of 0 bytes to fail, then
  28. *               set this flag.
  29. *       MemTag - pointer to an array of struct MemTag.
  30. *                  vt_Ptr is the address of a pointer.
  31. *                  vt_Size is the size of the allocation.
  32. *                  vt_MemFlags are the exec memory (MEMF_) flags. 
  33. *                Last tag should have vt_Ptr = NULL.
  34. *                 
  35. *   RESULT
  36. *       zero if it couldn't allocate the requested memory. or non-zero
  37. *       on success. vt_Ptrs will be point to a allocated 
  38. *       memory chunk or NULL.  
  39. *
  40. *   EXAMPLE
  41. *       EX1:
  42. *         struct foo *bar;
  43. *         STRPTR dest;
  44. *         APTR cow;
  45. *
  46. *        if( MultiAllocMem(0,
  47. *                           &bar,  sizeof(struct foo),  MEMF_CLEAR,
  48. *                           &dest, 25,                  MEMF_PUBLIC,
  49. *                           &cow,  100,                 MEMF_FAST|MEMF_PUBLIC,
  50. *                           0))
  51. *         {
  52. *           ...
  53. *           MultiFreeMem(3,
  54. *                           bar  ,sizeof(struct foo),
  55. *                           dest ,25,
  56. *                           cow  ,100);
  57. *         }
  58. *
  59. *       EX2: This will never fail.
  60. *         APTR foo;
  61. *         if(MultiAllocMem(0,
  62. *                           &foo,0,MEMF_CLEAR,
  63. *                           0)
  64. *         {...}
  65. *
  66. *       EX3: This will always fail.
  67. *         APTR foo;
  68. *         if(MultiAllocMem(MA_FAILSIZE0,
  69. *                           &foo,0,MEMF_CLEAR,
  70. *                           0)
  71. *         {...}
  72. *
  73. *   NOTES
  74. *       requires exec.library to be open.
  75. *
  76. *       if the MA_FAILSIZE0 Flag is not set, 0 byte allocations
  77. *       will pass even though no memory will be allocated for that.
  78. *       entry and mt_Ptr will be set to 0.
  79. *
  80. *       The memory allocated may be freed individually with 
  81. *       exec.library/FreeMem()
  82. *
  83. *   BUGS
  84. *
  85. *   SEE ALSO
  86. *       MultiAllocVecA(), MultiFreeVecA(), MultiFreeMemA(),
  87. *       MultiAllocPooledA(), MultiFreePooledA(),
  88. *       exec.library/AllocVec(), exec.library/FreeVec()
  89. *       exec.library/AllocMem(), exec.library/FreeMem()
  90. *       exec.library/AllocPooled(), exec.library/FreePooled()
  91. ******************************************************************************
  92. *
  93. */
  94.  
  95. BOOL MultiAllocMem(ULONG Flags, ULONG MemTag, ... )
  96. {
  97.   return(MultiAllocMemA(Flags,(struct MemTag *)&MemTag));
  98. }
  99.  
  100. BOOL MultiAllocMemA(ULONG Flags, struct MemTag *MemTagList)
  101. {
  102.   struct MemTag *tag;
  103.   
  104.   if(MemTagList)
  105.   {
  106.     tag=MemTagList;
  107.     while(tag->mt_Ptr)
  108.     {
  109.       *tag->mt_Ptr=0;
  110.       tag++;
  111.     }
  112.  
  113.     tag=MemTagList;
  114.     
  115.     if(Flags & MA_FAILSIZE0)
  116.     {
  117.       while(tag->mt_Ptr)
  118.       {
  119.         if(tag->mt_Size==0) return(FALSE);
  120.         tag++;
  121.       }
  122.       tag=MemTagList;
  123.     }
  124.        
  125.     while(tag->mt_Ptr)
  126.     {
  127.       if(tag->mt_Size)
  128.       {
  129.         if(!(*tag->mt_Ptr=AllocMem(tag->mt_Size,tag->mt_MemFlags)))
  130.         {
  131.           tag=MemTagList;
  132.           while(tag->mt_Ptr)
  133.           {
  134.             if(tag->mt_Ptr) FreeMem(*tag->mt_Ptr,tag->mt_Size);
  135.             tag++;
  136.           }
  137.           return(FALSE);     
  138.         }
  139.       }
  140.       else
  141.         *tag->mt_Ptr=0;
  142.       
  143.       tag++;
  144.     }
  145.     return(TRUE);
  146.   }
  147.   return(FALSE);
  148. }
  149.  
  150. /****** extras.lib/MultiFreeMemA ******************************************
  151. *
  152. *   NAME
  153. *       MultiFreeMemA -- Free multiple memory chunks.
  154. *       MultiFreeMem -- varargs stub.
  155. *
  156. *   SYNOPSIS
  157. *       MultiFreeMemA(Args, FreeTagList)
  158. *
  159. *       void MultiFreeMemA(ULONG, struct FreeTag *);
  160. *
  161. *       MultiFreeMem(Args, FreeTag, ... )
  162. *
  163. *       void MultiFreeMem(ULONG, ULONG, ... );
  164. *
  165. *   FUNCTION
  166. *       Free multiple memory blocks allocated with MultiAllocMemA()
  167. *       or exec.library/AllocMem(). 
  168. *
  169. *   INPUTS
  170. *       Args - Number of blocks that are to be freed.
  171. *       FreeTagList - An array of FreeTags. may be NULL.
  172. *                       ft_Ptr - contains a pointer to a
  173. *                                memory block or NULL.
  174. *                       ft_Size - the size of the block.
  175. *
  176. *   RESULT
  177. *       none.
  178. *
  179. *   NOTES
  180. *       requires exec.library to be open.
  181. *
  182. *   SEE ALSO
  183. *       MultiAllocVecA(), MultiFreeVecA(), MultiAllocMemA(),
  184. *       MultiAllocPooledA(), MultiFreePooledA(),
  185. *       exec.library/AllocVec(), exec.library/FreeVec()
  186. *       exec.library/AllocMem(), exec.library/FreeMem()
  187. *       exec.library/AllocPooled(), exec.library/FreePooled()
  188. *
  189. ******************************************************************************
  190. *
  191. */
  192.  
  193. void MultiFreeMem(ULONG Args, ULONG FreeTag, ... )
  194. {
  195.   MultiFreeMemA(Args,(struct FreeTag *)&FreeTag);
  196. }
  197.  
  198. void MultiFreeMemA(ULONG Args, struct FreeTag *FreeTagList)
  199. {
  200.   LONG l;
  201.   
  202.   for(l=0;l<Args;l++)
  203.   {
  204.     FreeMem(FreeTagList[l].ft_Ptr, FreeTagList[l].ft_Size);
  205.   }
  206. }
  207.